<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 角色管理 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="showDialog = true"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table border="" :data="roleList">
              <el-table-column type="index" label="序号" width="120" />
              <el-table-column prop="name" label="角色名称" width="240" />
              <el-table-column prop="description" label="描述" />
              <el-table-column label="操作">
                <template #default="{row}">
                  <el-button size="small" type="success" @click="assignPerm(row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="editRole(row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="delRole(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination
                layout="prev,pager,next"
                :total="page.total"
                :page-size="page.pagesize"
                @current-change="currentChange"
              />
            </el-row>
          </el-tab-pane>
          <!-- 公司信息 -->
          <el-tab-pane label="公司信息">
            <el-alert
              title=" 公司信息不可修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="公司名称">
                <el-input v-model="formData.name" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="formData.companyAddress" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="formData.mailbox" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="formData.remarks" type="textarea" :rows="3" disabled style="width:400px" />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <!-- 编辑弹窗 -->
      <el-dialog :title="titleText" :visible="showDialog" @close="btnCancel">
        <el-form v-if="showDialog" ref="roleForm" :model="roleForm" :rules="rules" label-width="120px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" @click="btnCancel">取消</el-button>
            <el-button size="small" type="primary" @click="btnOK">确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>
      <!-- 分配权限 -->
      <el-dialog title="分配权限" :visible="showPermDialog" @close="btnPermCancel">
        <!-- 权限是一颗树 -->
        <!-- 将数据绑定到组件上 -->
        <!-- check-strictly 如果为true 那表示父子勾选时  不互相关联 如果为false就互相关联 -->
        <!-- id作为唯一标识 :default-checked-keys="selectCheck" -->
        <el-tree
          ref="permTree"
          :data="permData"
          :props="{label:'name'}"
          :show-checkbox="true"
          :check-strictly="true"
          :default-expand-all="true"

          node-key="id"
        />
        <!-- 确定 取消 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button type="primary" size="small" @click="btnPermOK">确定</el-button>
            <el-button size="small" @click="btnPermCancel">取消</el-button>
          </el-col>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getCompanyInfo, getRoleList, delRole, getRoleDetail, updateRole, addRole, assignPermission } from '@/api/setting'
import { getPermissionList } from '@/api/permission'
import { listToTreeData } from '@/utils'
export default {
  data() {
    return {
      showPermDialog: false,
      permData: [],
      userId: '',

      // 公司信息
      formData: {
        name: '',
        companyAddress: '',
        mailbox: '',
        remarks: ''
      },
      // 角色列表 -- 数组
      roleList: [],
      // 翻页设定
      page: {
        page: 1, // 页码
        pagesize: 8, // 每页条数
        total: 0// 记录总数
      },
      // 编辑弹窗列表
      roleForm: {
        name: '',
        description: ''
      },
      showDialog: false,

      rules: {
        name: [
          { required: true, message: '该项不能为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '该项不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    titleText() {
      return this.roleForm.id ? '编辑角色' : '新增角色'
    }
  },
  created() {
    this.getCompanyInfo()
    this.getRoleList()
  },
  methods: {
    async assignPerm(id) {
      // 记录点击的角色的id
      this.userId = id
      // 1、获取全部权限列表
      this.permData = listToTreeData(await getPermissionList(), '0')
      // 2、获取当前用户点击的权限
      const { permIds } = await getRoleDetail(id)
      // 3、弹出弹窗
      this.showPermDialog = true
      this.$nextTick(() => { this.$refs.permTree.setCheckedKeys(permIds) })
    },
    btnPermCancel() {
      // 1、清空数据
      this.$refs.permTree.setCheckedKeys([])
      // 2、关闭弹窗
      this.showPermDialog = false
    },
    async btnPermOK() {
      // 发请求
      // 1、拿到数据
      const permIds = this.$refs.permTree.getCheckedKeys()
      const id = this.userId
      // console.log(permIds, id)
      // 2、发送api请求
      await assignPermission({
        permIds, id
      })
      // 3、提醒用户
      this.$message.success('修改成功')
      // 4、关闭弹窗
      this.showPermDialog = false
    },

    // 获取公司信息
    async getCompanyInfo() {
      this.formData = await getCompanyInfo(this.$store.state.user.userInfo.companyId)
    },
    // 获取角色列表
    async getRoleList() {
      // rows:每页行数
      const { total, rows } = await getRoleList(this.page)
      // console.log(res)
      this.page.total = total
      this.roleList = rows
    },
    // 分页功能
    currentChange(newPage) {
      this.page.page = newPage
      this.getRoleList()
    },
    // 删除角色功能
    async delRole(id) {
      // 二次校验
      await this.$confirm('是否确认删除该角色')
      // 发请求
      await delRole(id)
      // 如果最后一页最后一个删完，页码自动往前跳一页
      if (this.roleForm.length === 1 && this.page.page > 1) {
        this.page.page--
      }
      // 提示
      this.$message.success('删除成功')
      // 重新加载数据
      this.getRoleList()
    },
    async editRole(id) {
      // 1、回显数据
      this.roleForm = await getRoleDetail(id)
      // 2、显示弹窗
      this.showDialog = true
    },
    // 弹窗确认按钮
    async btnOK() {
      try {
        // 校验
        await this.$refs.roleForm.validate()
        // 发送请求
        if (this.roleForm.id) {
        // 编辑
          await updateRole(this.roleForm)
        } else {
        // 新增
          await addRole(this.roleForm)
        }
        // 提示用户
        this.$message.success('操作成功')
        // 关闭弹窗
        this.showDialog = false
        // 重新家在数据
        this.getRoleList()
      } catch (error) {
        console.log(error)
      }
    },
    // 弹窗取消按钮
    btnCancel() {
      // 强制清理表单
      this.roleForm = {
        name: '',
        description: ''
      }
      // 清理校验
      this.$refs.roleForm.resetFields()
      // 关闭弹窗
      this.showDialog = false
    }
  }
}
</script>res

<style>

</style>
